/*
 * @Author: Lwz
 * @Coding: utf-8
 * @Date: 2019-12-20
 */

const ImageICON = {
   picture: {
      folder: '/spa/document/images/file.png',
      file: {
         image: '/spa/document/images/netdisk/picture/image.png',
         excel: '/spa/document/images/netdisk/picture/excel.png',
         word: '/spa/document/images/netdisk/picture/word.png',
         ppt: '/spa/document/images/netdisk/picture/ppt.png',
         pdf: '/spa/document/images/netdisk/picture/pdf2.png',
         txt: '/spa/document/images/netdisk/picture/txt.png',
         video: '/spa/document/images/netdisk/picture/video.png',
         audio: '/spa/document/images/netdisk/picture/audio.png',
         compressed: '/spa/document/images/netdisk/picture/zip2.png',
         code: '/spa/document/images/netdisk/picture/code.png',
         others: '/spa/document/images/netdisk/picture/otherfile.png',
         default: '/spa/document/images/netdisk/picture/otherfile2.png',
      },
   },
   thumbnail: {
      folder: '/spa/document/images/file.png',
      file: {
         image: '/spa/document/images/netdisk/thumbnail/image.png',
         excel: '/spa/document/images/netdisk/thumbnail/excel.png',
         word: '/spa/document/images/netdisk/thumbnail/word.png',
         ppt: '/spa/document/images/netdisk/thumbnail/ppt.png',
         pdf: '/spa/document/images/netdisk/thumbnail/pdf2.png',
         txt: '/spa/document/images/netdisk/thumbnail/txt2.png',
         video: '/spa/document/images/netdisk/thumbnail/video.png',
         audio: '/spa/document/images/netdisk/thumbnail/audio.png',
         compressed: '/spa/document/images/netdisk/thumbnail/zip.png',
         code: '/spa/document/images/netdisk/thumbnail/code.png',
         others: '/spa/document/images/netdisk/thumbnail/otherfile3.png',
         default: '/spa/document/images/netdisk/thumbnail/otherfile.png',
      },
   },
};

const SvgICON = {
   picture: {
      folder: '/spa/document/images/file.png',
      file: {
         image: { icon: 'icon-coms-pic2', color: '#009EFB' },
         excel: { icon: 'icon-coms-excel2', color: '#64D16F' },
         word: { icon: 'icon-coms-word2', color: '#0176FA' },
         ppt: { icon: 'icon-coms-ppt2', color: '#8A8C8E' },
         pdf: { icon: 'icon-coms-pdf2', color: '#DF4430' },
         txt: { icon: 'icon-coms-content-o', color: '#FD6950' },
         video: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         audio: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         compressed: { icon: 'icon-coms-rar2', color: '#716BFF' },
         code: { icon: 'icon-coms-html2', color: '#FFBB32' },
         others: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         default: { icon: 'icon-coms-currency2', color: '#8B96C2' },
      },
   },
   thumbnail: {
      folder: '/spa/document/images/file.png',
      file: {
         image: { icon: 'icon-coms-pic2', color: '#009EFB' },
         excel: { icon: 'icon-coms-excel2', color: '#64D16F' },
         word: { icon: 'icon-coms-word2', color: '#0176FA' },
         ppt: { icon: 'icon-coms-ppt2', color: '#8A8C8E' },
         pdf: { icon: 'icon-coms-pdf2', color: '#DF4430' },
         txt: { icon: 'icon-coms-content-o', color: '#FD6950' },
         video: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         audio: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         compressed: { icon: 'icon-coms-rar2', color: '#716BFF' },
         code: { icon: 'icon-coms-html2', color: '#FFBB32' },
         others: { icon: 'icon-coms-currency2', color: '#8B96C2' },
         default: { icon: 'icon-coms-currency2', color: '#8B96C2' },
      },
   },
};

const extLibrary = {
   image: ['jpg', 'bmp', 'gif', 'jpeg', 'png', 'psd', 'tiff', 'raw'],
   excel: ['xls', 'xlsx'],
   word: ['doc', 'docx', 'wps'],
   ppt: ['ppt', 'pptx'],
   pdf: ['pdf'],
   txt: ['txt'],
   video: ['avi', 'mov', 'rm', 'rmvb', 'wmv', '3gp', 'mpg', 'mpeg', 'mkv', 'flv', 'mp4'],
   audio: ['mp3', 'wav', 'wma', 'mod', 'ra', 'cmf', 'mid', 'midi', 'ape', 'aac', 'flac'],
   compressed: ['rar', 'zip', 'jar', '7z', 'acb', 'iso', 'tar', 'uue'],
   code: ['c', 'java', 'class', 'jsp', 'php', 'html', 'css', 'less', 'sass', 'js', 'jsx', 'ts', 'tsx', 'json', 'py', 'pyi'],
};

const throwError = (string) => {
 throw new Error(string); 
};

// 判断文件类型 【参数：文件全名。返回：文件类型】
const judgeFileType = (fileName) => {
   let fileType = '';
   const ext = fileName.split('.').pop().toLowerCase();
   // 判断的逻辑
   for (let i in extLibrary) {
      if (extLibrary[i].includes(ext)) {
         fileType = i;
         break;
      }
   }
   if (fileType.length === 0) {
      fileType = 'others';
   }
   return fileType;
};

// 根据文件类型判断使用哪种图标 【返回：icon】
const renderIcon = (type, fileType, applyType, iconType) => {
   let icon = '';
   const iconLib = iconType === 'img' ? ImageICON : SvgICON;
   if (type === 'folder') {
      icon = iconLib[applyType].folder;
   } else if (type === 'file') {
      icon = Object.prototype.hasOwnProperty.call(iconLib[applyType].file, fileType) ? iconLib[applyType].file[fileType] : iconLib[applyType].file.default;
   }
   return icon;
};

const paramCheck = (type, fileName, applyType, iconType) => {
   let msg = 'document -> public -> utils -> fileType参数检查错误：';
   if (!['thumbnail', 'picture'].includes(applyType)) {
      throwError(`${msg} applyType必须是 thumbnail 或 picture`);
   }
   if (fileName.length === 0) {
      throwError(`${msg} 文件名不能为空`);
   }
   if (!['folder', 'file'].includes(type)) {
      throwError(`${msg} type必须是 folder 或 file`);
   }
   if (!['img', 'svg'].includes(iconType)) {
      throwError(`${msg} iconType必须是 img 或 svg`);
   }
};


// 根据文件名判断使用的 【参数：类型(file or folder)、文件全名、应用类型。返回：文件图标】
/*
* type: file or folder
* fileName: 文件全名（含扩展名）
* applyType: thumbnail or picture 应用类型：缩略图还是大图
* iconType: 图片还是SVG图标 img or svg
*
* */
const getFileIcon = (type, fileName, applyType, iconType) => {
   paramCheck(type, fileName, applyType, iconType); // 参数检查
   const fileType = judgeFileType(fileName);
   return renderIcon(type, fileType, applyType, iconType);
};


export default getFileIcon;
